package bravo.xc.com.xc_bravo_android.operation.login;


import android.content.Context;
import android.util.Log;

import bravo.xc.com.xc_bravo_android.beans.LoginBeans;
import bravo.xc.com.xc_bravo_android.beans.LoginResultBeans;
import bravo.xc.com.xc_bravo_android.http.HttpSingleton;
import bravo.xc.com.xc_bravo_android.http.UserOperationService;
import cn.jpush.android.api.JPushInterface;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGINOUT;

/**
 * CREATED BY YANK
 * DATE : 2017/10/27
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class LoginPresenter extends LoginContract.LoginPresenter {

    private UserOperationService userOperationService;

    @Override
    public void doLogin(LoginBeans user, final Context contexts) {

        userOperationService = HttpSingleton.getInstance().service;
        compositeSubscription.add(userOperationService.login(user)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResultBeans>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@LoginError",String.valueOf(e));
                        view.showToast("网络错误，请重试");
                        view.showProgress(false);
                        view.setNull(2);
                        JPushInterface.deleteAlias(contexts,LOGINOUT);
                    }

                    @Override
                    public void onNext(LoginResultBeans loginResultBeans) {
                        Log.e("@@LoginResults",String.valueOf(loginResultBeans.getCode()));

                        if (0 == loginResultBeans.getCode()){
                            view.showProgress(false);
                            view.setNull(0);
                            view.showToast("未知错误，请重试");
                            JPushInterface.deleteAlias(contexts,LOGINOUT);
                        }
                        if (1 == loginResultBeans.getCode()){
                            view.setAlia();
                            view.setSP(loginResultBeans);
                            view.showProgress(false);
                            view.setNull(0);
                            view.toNewActivity(1);
                        }
                        if (2 == loginResultBeans.getCode()) {
                            view.showProgress(false);
                            view.setNull(2);
                            view.showToast("请检查您的用户名");
                            JPushInterface.deleteAlias(contexts,LOGINOUT);
                        }
                        if (3 == loginResultBeans.getCode()){
                            view.showProgress(false);
                            view.setNull(1);
                            view.showToast("密码错误，请重试");
                            JPushInterface.deleteAlias(contexts,LOGINOUT);
                        }
                        if (4 == loginResultBeans.getCode()){
                            view.showProgress(false);
                            view.setNull(1);
                            view.showToast("此账号已经登陆");
                            JPushInterface.deleteAlias(contexts,LOGINOUT);
                        }

                    }
                }));
    }
}
